<template>
  <rx-dialog @handOk="okFun" @cancel="cancel">
    <rx-layout>
      <div slot="center">
        <rx-fit>
          <div slot="toolheader" foldbtn="false">
            <a-form :form="form" layout="inline" style="width: 100%">
                {{i18n('list.formSelect','表单选择：')}}

              <a-select id="tempType" :getPopupContainer="p=>p.parentNode" v-model="selectType" style="width: 80%" @change="defChangeType">
                <a-select-option v-for="item in formBoEntityList" :key="item.id" :value="item.id">{{item.name}}
                </a-select-option>
              </a-select>
            </a-form>
          </div>

          <rx-grid
            ref="table"
            :dataSource="headerFields"
            :allowRowSelect="true"
            :multiSelect="true"
            :showPage="true"
            :pageSizeOptions="['10','30','40']"
            :columns="columns"
          >
          </rx-grid>
        </rx-fit>
      </div>
    </rx-layout>

  </rx-dialog>
</template>
<script>
  import {RxDialog,BaseForm, RxLayout, RxGrid,Util,RxFit} from "@lowcode/jpaas-common-lib";
  import FormBoAttrApi from "@/api/form/bo/formBoAttr";
  export default {
    name: 'FormPcRightViews',
    mixins: [BaseForm],
    props:{
      layerid: {
        type: String,
        default: ""
      },
      destroy: {
        type: Function
      },
      boDefId:{
        type: String,
        default: ""
      }
    },
    components: {
      RxLayout,
      RxGrid,
      RxDialog,
      Util,
      RxFit,
    },
    data() {
      return {
        selectType:"",
        formBoEntityList: [],
        headerFields: [],
        columns: [
          {
            key:'01',
            title: this.$ti18('rxList.index','序号'),
            type:'indexColumn',
            width: '15%',
            dataIndex:'serial',
            scopedSlots: { customRender: 'serial' },
          },
          {
            key:'02',
            title: this.i18n('form.fieldName','字段名称'),
            width: '45%',
            dataIndex: 'comment',
          },
          {
            key:'03',
            title: this.i18n('form.fieldKey','字段key'),
            width: '40%',
            dataIndex: 'name'
          }
        ]
      }
    },
    created() {
      this.queryToBoEntity();
    },
    methods: {
      i18n(name,text,key){
          return this.$ti18(name,text,"formBoAttr",key);
      },
      queryToBoEntity:function(){
        var self_=this;
        FormBoAttrApi.queryToBoEntity(this.boDefId).then (res => {
          this.formBoEntityList = res;
          if(res && res.length>0){
            var val =res[0].id;
            self_.selectType=val;
            self_.defChangeType(val);
          }
        })
      },
      defChangeType:function(val){
        var self_=this;
        FormBoAttrApi.getByEntId(val).then (res => {
          self_.headerFields = res;
        })
      },
      addForm:function () {
        var self_=this;
      },
      getFormBoEntity(){
        for(var i=0;i<this.formBoEntityList.length;i++){
          var formBoEntity=this.formBoEntityList[i];
          if(this.selectType==formBoEntity.id){
            return formBoEntity;
          }
        }
      },
      getSelectData(){
        var returnData={};
        if(!this.formBoEntityList || this.formBoEntityList.length==0){
          return returnData;
        }
        var selectData =this.$refs.table.getSelectedRows();
        if(!selectData || selectData.length==0){
          return returnData;
        }
        var formBoEntity=this.getFormBoEntity();
        returnData["entName"]=formBoEntity.alias;
        returnData["type"]=formBoEntity.boRelation.type;
        var returnArray = [];
        for(var i=0;i<selectData.length;i++){
          var data=selectData[i];
          var returnObj={
            "uid": Util.randomId(),
            "label": data.comment,
            "field": data.name,
            "key": data.name,
            "datatype": data.dataType,
            "isSingle": data.isSingle,
            "expression": ""
          }
          returnArray.push(returnObj);
        }
        returnData["returnArray"]=returnArray;
        return returnData;
      },
      okFun(){
        Util.closeWindow(this,"ok",this.getSelectData());
      }
    }

  }
</script>
<style scoped>
</style>